//
//  MYGHomePageViewController.swift
//  MYDoctor
//
//  Created by Mo on 2024/3/6.
//

import UIKit

class MYGHomePageViewController: MYGBaseController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        getData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        cusTitleView.userHeaderView.sd_setImage(with:URL(string: MYGUserManager.getUser()?.doctor.avatar), placeholderImage: UIImage.init(named: "head"), context: nil)
        cusTitleView.userNameLabel.text = MYGUserManager.getUser()?.doctor.name
        cusTitleView.userIdentityLabel.text = MYGUserManager.getUser()?.doctor.department
    }
    
    var cusTitleView: MYGHomePageTitleView = {
        let control = MYGHomePageTitleView.init(frame: CGRect(x: 0, y: 0, width: screenWidth, height: navHeight))
        control.translatesAutoresizingMaskIntoConstraints = false
        return control
    }()
    func getCurrentDate()->String {
        let dateformatter = DateFormatter()
        dateformatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
        dateformatter.dateFormat = "yyyy-MM-dd"// 自定义时间格式
        return dateformatter.string(from: Date())
    }
    
    func getData() {
        let par = [
            "set_date":getCurrentDate()]
        DennyNetwork.request(MYGHomePageAPI.scheduleDay(param: par), modelType: [MYGHomeScheduleListModel].self) { obj in
                if let first = obj.first {
                    self.headerView.bindData(models: first.lists)
                }
            } failed: { code, message in
               
            }
    }
    func initUI(){
        self.navigationItem.titleView = self.cusTitleView
        
        view.addSubview(mainScrollView)
        mainScrollView.addSubview(mainContainerView)
        mainContainerView.addSubview(headerView)
        mainContainerView.addSubview(hotNewView)
        mainContainerView.addSubview(activityView)

        mainScrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        mainContainerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(screenWidth)
        }
        headerView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(420)
        }
        hotNewView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(headerView.snp.bottom)
            make.height.equalTo(SCALEWIDTH(580))
        }
        activityView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(hotNewView.snp.bottom)
            make.height.equalTo(368)
            make.bottom.equalToSuperview()
        }
    }
    lazy var mainScrollView: UIScrollView = {
        var view = UIScrollView()
        view.backgroundColor = RGBA(r: 245, g: 246, b: 248, a: 1)
        view.showsVerticalScrollIndicator = false
        return view
    }()
    
    lazy var mainContainerView: UIView = {
        var view = UIView()
        view.backgroundColor = RGBA(r: 245, g: 246, b: 248, a: 1)
        return view
    }()
    
    lazy var headerView:MYGHomePageHeaderView = {
        var view = MYGHomePageHeaderView()
        view.toolsBlock = {[weak self] in
            self?.navigationController?.pushViewController(MYGToolsViewController(), animated: true)
        }
        return view
    }()
    
    lazy var hotNewView:MYGHotNewsView = {
        var view = MYGHotNewsView()
        return view
    }()
    
    lazy var activityView:MYGActivityView = {
        var view = MYGActivityView()
        view.tapTopIndexCompletion = { contentHight in
            view.snp.updateConstraints { make in
                make.height.equalTo(contentHight)
            }
        }
        return view
    }()
}
